package com.groupbuying.payconsumer.wxpay;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.util.Map;

import static com.groupbuying.payconsumer.wxpay.WXPayConstants.USER_AGENT;


/**
 * @Author gxy
 * @Description 微信支付调用微信相关接口
 * @Date 2019/2/22 8:59
 **/
public class WXPay {

//    /**
//     * @Author gxy
//     * @Description 统一下单接口
//     * @Date 9:00 2019/2/22
//     **/
//
//    public static Map<String, String> unifiedOrder(Map<String, String> reqData) throws Exception {
//        //调起支付API的ip地址
//        reqData.put("spbill_create_ip",WXPayUtil.getLocalIP());
//        //支付异步通知地址
//        reqData.put("notify_url",WXPayMsgConfig.NOTIFY_URL);
//        return smallAppBaseRequest(reqData, WXPayMsgConfig.PAY_API_URL);
//    }

    /**
     * 查询订单
     * @param param
     * @return
     * @throws Exception
     */
    public static Map<String,String> payOrderquery(Map<String,String> param) throws Exception{
        return smallAppBaseRequest(param, WXPayMsgConfig.ORDER_QUERY_API);

    }

    /**
     * 关闭订单
     * @param param
     * @return
     * @throws Exception
     */
    public static Map<String,String> closePayOrder(Map<String,String> param) throws Exception{
        return smallAppBaseRequest(param, WXPayMsgConfig.CLOSE_API);
    }

    /**
     * 发起退款
     * @param param
     * @return
     * @throws Exception
     */
    public static Map<String,String> applyForRefund(Map<String,String> param) throws Exception{
        return smallAppBaseRequest(param, WXPayMsgConfig.REFUND_API);
    }

    /**
     * 退款查询
     * @param reqData
     * @return
     * @throws Exception
     */
    public static Map<String, String> refundQuery(Map<String, String> reqData) throws Exception {
        return smallAppBaseRequest(reqData,WXPayMsgConfig.REFUND_QUERY_API);
    }

    /**
     * 小程序支付的基础请求接口
     * @param param 请求参数
     * @param url 请求地址
     * @return
     * @throws Exception
     */
    public static Map<String,String> smallAppBaseRequest(Map<String,String> param,String url) throws Exception{
        param.put("appid",WXPayMsgConfig.SMALL_APP_ID);
        param.put("mch_id",WXPayMsgConfig.MCH_ID);
        param.put("nonce_str",WXPayUtil.generateNonceStr());
        param.put("sign_type",WXPayMsgConfig.SIGN_TYPE);
        param.put("sign",WXPayUtil.generateSignature(param, WXPayMsgConfig.KEY));
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        String data = MapToXMLString.converter(param);
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(60000).build();
        httpPost.setConfig(requestConfig);
        StringEntity postEntity = new StringEntity(data, "UTF-8");
        httpPost.addHeader("Content-Type", "text/xml");
        httpPost.addHeader("User-Agent", USER_AGENT + " " + WXPayMsgConfig.MCH_ID);
        httpPost.setEntity(postEntity);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        String response = EntityUtils.toString(httpEntity,"UTF-8");
        Map map = WXPayUtil.xmlToMap(response);
        return map;
    }

}
